package com.example.wm.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.wm.common.Result;
import com.example.wm.common.ResultData;
import com.example.wm.entity.LeaveInfomation;

import com.example.wm.mapper.QjmessageMapper;
import com.example.wm.service.QjmessageService;
import com.example.wm.utils.TsyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/qjmessage")
@CrossOrigin
public class QjmessageController {

    @Resource
    private QjmessageService qjmessageService;
    @Resource
    private QjmessageMapper qjmessageMapper;
    @Resource
    private TsyUtils tsyUtils;
        @Transactional
    @PostMapping("/setqjmeassage")
    public Result saveqjmeassage(@RequestBody LeaveInfomation qj){
       qj.setReviewtime("0000-00-00 00:00:00");
          Boolean b=  qjmessageService.save(qj);
            return ResultData.success(b);
        }

    @Transactional
    @PostMapping("/name")
    public Result findmessage(@RequestBody JSONObject jsonObject) {
        // 接收传入数据
        String name = jsonObject.getString("name");
        if (name == null) {
            return ResultData.error(-1,"请检查参数是否正确");
        }
        // 创建查询构造器
        LambdaQueryWrapper<LeaveInfomation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LeaveInfomation::getName,name);
        queryWrapper.ne(LeaveInfomation::getStatus,"已撤回");
        queryWrapper.orderByDesc(LeaveInfomation::getApplicationtime);

        // 调用mapper查询数据
        List<LeaveInfomation> list = qjmessageMapper.selectList(queryWrapper);
        // 返回数据
        return ResultData.success("操作成功",list);
    }

    @Transactional
    @PostMapping("/findAll")
    public  Result qjfindAll(){
        // 创建查询构造器
        LambdaQueryWrapper<LeaveInfomation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.last("order by 开始时间 desc");
        // 调用mapper查询数据
        List<LeaveInfomation> list = qjmessageMapper.selectList(queryWrapper);
        return ResultData.success("操作成功",list);
    }

    @Transactional
    @PostMapping("/qjapprove")
    public Result qjapprove(@RequestBody LeaveInfomation leaveInfomation){

            leaveInfomation.setReviewtime(tsyUtils.getnowtime());
    qjmessageService.updateById(leaveInfomation);
        return ResultData.success("修改成功",1);
    }
    //已经审批
    @Transactional
    @PostMapping("/approveysp")
    public Result approve_ysp(@RequestBody JSONObject jsonObject){
        String name=jsonObject.getString("name");
        Date d1=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String strDate=sdf.format(d1);
        log.info(strDate);
            LambdaQueryWrapper<LeaveInfomation> lq=new LambdaQueryWrapper<>();
            lq.eq(LeaveInfomation::getApprovepp,name);
            lq.ne(LeaveInfomation::getStatus,"待审批");
            lq.between(LeaveInfomation::getApplicationtime,strDate+"-01 00:00:00",strDate+"-31 00:00:00");
            lq.orderByDesc(LeaveInfomation::getReviewtime);
        List<LeaveInfomation> list=qjmessageMapper.selectList(lq);
        return ResultData.success(list);
    }

    //已经审批 查询本月份全部已审批的人
    @Transactional
    @PostMapping("/approveysp_all")
    public Result approveysp_all(@RequestBody JSONObject jsonObject){
String strDate=jsonObject.getString("strDate");
String employeename=jsonObject.getString("employeename");
        log.info(strDate);
        LambdaQueryWrapper<LeaveInfomation> lq=new LambdaQueryWrapper<>();

        lq.eq(LeaveInfomation::getStatus,"已批准");
        lq.eq(LeaveInfomation::getName,employeename);
        lq.between(LeaveInfomation::getBegintime,strDate+"-01 00:00:00",strDate+"-31 00:00:00");
        lq.orderByDesc(LeaveInfomation::getReviewtime);
        List<LeaveInfomation> list=qjmessageMapper.selectList(lq);
        return ResultData.success(list);
    }
    //待审批
    @PostMapping("/approvewsp")
    public Result approve_wsp(@RequestBody JSONObject jsonObject){
        String name=jsonObject.getString("name");
//        Date d=new Date();
//        SimpleDateFormat  fm=new SimpleDateFormat("yyyy-MM");
//        String strDate=fm.format(d);
        LambdaQueryWrapper<LeaveInfomation> lq=new LambdaQueryWrapper<>();
        lq.eq(LeaveInfomation::getApprovepp,name);
        lq.eq(LeaveInfomation::getStatus,"待审批");
        List<LeaveInfomation> list=qjmessageMapper.selectList(lq);
        return ResultData.success(list);
    }
    //查询请假人的请假条数
    @PostMapping("/qj_number")
    public Result<Integer> qj_number(@RequestBody JSONObject jsonObject) {
        String strDate = jsonObject.getString("qjtime");
        String name2 = jsonObject.getString("name2");

        LambdaQueryWrapper<LeaveInfomation> lq2 = new LambdaQueryWrapper<>();
        lq2.eq(LeaveInfomation::getName, name2);
        lq2.eq(LeaveInfomation::getStatus,"已批准");
        lq2.between(LeaveInfomation::getBegintime, strDate + "-01 00:00:00", strDate + "-31 00:00:00");
        List<LeaveInfomation> list = qjmessageMapper.selectList(lq2);
        float n = 0;
        try {
            for (int i = 0; i <= list.size() - 1; i++) {
                n += list.get(i).getTimelong();
                list.get(i).setQjnumber(n);
            }
        } catch (IndexOutOfBoundsException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        return ResultData.success(n);
    }
    //搜索
    @PostMapping("/searchs")
    public Result searcs(@RequestBody JSONObject jsonObject){
        String name=jsonObject.getString("name");
        String strDate = jsonObject.getString("qjtime");

//        Date d=new Date();
//        SimpleDateFormat  fm=new SimpleDateFormat("yyyy-MM");
//        String strDate=fm.format(d);
        LambdaQueryWrapper<LeaveInfomation> lq=new LambdaQueryWrapper<>();
        if(!Objects.equals(name, "")){
            lq.like(LeaveInfomation::getName,name);
        }
        lq.eq(LeaveInfomation::getStatus,"已批准");
        lq.between(LeaveInfomation::getBegintime, strDate + "-01 00:00:00", strDate + "-31 00:00:00");

        List<LeaveInfomation> list=qjmessageMapper.selectList(lq);
        return ResultData.success(list);
    }
}
